﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Management;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace yichu_front
{
    public static class Pub
    {
        public static string username;
        public static string password;

        public static User user { get; set; }

        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
        ///// <summary>
        ///// 获取网卡地址
        ///// </summary>
        ///// <returns></returns>
        //public static string GetMacString()
        //{
        //    string resMac = "";
        //    ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
        //    ManagementObjectCollection moc2 = mc.GetInstances();
        //    foreach (ManagementObject mo in moc2)
        //    {
        //        if ((bool)mo["IPEnabled"] == true)
        //        {
        //            resMac = mo["MacAddress"].ToString();
        //            mo.Dispose();
        //            return resMac;
        //        }
        //    }
        //    return resMac;
        //}
        /// <summary>
        /// 获取网卡地址。但是可能会有虚拟网卡地址
        /// </summary>
        /// <returns></returns>
        //public static string GetMacAddress()
        //{
        //    System.Net.IPHostEntry myEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
        //    //string ipAddress = myEntry.AddressList[0].ToString();

        //    List<string> macList = new List<string>();

        //    foreach (var nic in NetworkInterface.GetAllNetworkInterfaces())
        //    {
        //        if (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
        //        {
        //            macList.Add(nic.GetPhysicalAddress().ToString());
        //        }
        //    }
        //    if (macList != null && macList.Count > 0)
        //    {
        //        //button1.Text = macList[0];
        //        //Console.WriteLine(macList[0]);
        //    }
        //    return macList[0];
        //}
        /// <summary>
        /// 获取真实的有线网卡地址
        /// </summary>
        /// <returns></returns>
        public static string GetMacAddressByNetworkInformation()
        {
            string key = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\";
            string macAddress = string.Empty;
            try
            {
                NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface adapter in nics)
                {
                    if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet
                        && adapter.GetPhysicalAddress().ToString().Length != 0)
                    {
                        string fRegistryKey = key + adapter.Id + "\\Connection";
                        RegistryKey rk = Registry.LocalMachine.OpenSubKey(fRegistryKey, false);
                        if (rk != null)
                        {
                            string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString();
                            int fMediaSubType = Convert.ToInt32(rk.GetValue("MediaSubType", 0));
                            if (fPnpInstanceID.Length > 3 &&
                                fPnpInstanceID.Substring(0, 3) == "PCI")
                            {
                                macAddress = adapter.GetPhysicalAddress().ToString();
                                for (int i = 1; i < 6; i++)
                                {
                                    macAddress = macAddress.Insert(3 * i - 1, ":");
                                }
                                break;
                            }
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                //这里写异常的处理
            }
            return macAddress;
        }

        /// <summary>
        /// 关闭excel进程
        /// </summary>
        /// <param name="excel"></param>
        //public static void kill(Microsoft.Office.Interop.Excel.Application excel)
        //{
        //    IntPtr t = new IntPtr(excel.Hwnd);//得到这个句柄，具体作用是得到这块内存入口 
        //    int k = 0;
        //    GetWindowThreadProcessId(t, out k);   //得到本进程唯一标志k
        //    System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);   //得到对进程k的引用
        //    p.Kill();     //关闭进程k
        //}

        public static bool checkIsNumber(string columnsName, string value)
        {
            //如果传入的列 规格，编码，导体折数，额定电压，耐压等级则不验证是不是数字格式
            if (columnsName != "规格" && columnsName != "编码" && columnsName != "导体折数" && columnsName != "额定电压(V)" && columnsName != "耐压等级(V)")
            {
                decimal i = 0;
                return decimal.TryParse(value, out i);
            }
            else
            {
                return true;
            }

        }
        /// <summary>
        /// 把用的材料拼接到表头
        /// </summary>
        /// <param name="bOMModelid"></param>
        /// <param name="BOMListDataTable"></param>
        /// <returns></returns>
        //public static DataTable appendMaterialTableHeader(int bOMModelid, DataTable BOMListDataTable)
        //{
        //    //根据bomModelId查询所需材料的信息
        //    List<BOM_Material_Mapping> BOM_Material_MappingList = BOMService.getMaterial(bOMModelid, 0);
        //    //根据查到的材料，加到规格表的表头
        //    foreach (BOM_Material_Mapping BOM_Material_Mapping in BOM_Material_MappingList)
        //    {
        //        //如果没有这个表头，就添加表头
        //        if (!BOMListDataTable.Columns.Contains(BOM_Material_Mapping.材料名称))
        //        {
        //            //新增类型对应用料表的ID列
        //            BOMListDataTable.Columns.Add(BOM_Material_Mapping.材料名称);
        //        }
        //    }

        //    return BOMListDataTable;
        //}

        //public static void exportDataTable(DataTable dt, string fileName)
        //{
        //    FolderBrowserDialog dialog = new FolderBrowserDialog();
        //    dialog.Description = "请选择文件路径";

        //    if (dialog.ShowDialog() == DialogResult.OK)
        //    {
        //        string foldPath = dialog.SelectedPath;
        //        var path = Path.Combine(foldPath, $"{DateTime.Now.ToString("yyyy-MM-dd") + fileName}.xlsx");
        //        MiniExcel.SaveAs(path, dt);
        //    }
        //}
        public static T ToDataDto<T>(this DataTable dt)
        {
            T s = Activator.CreateInstance<T>();
            if (dt == null || dt.Rows.Count == 0)
            {
                return s;
            }
            var plist = new List<PropertyInfo>(typeof(T).GetProperties());
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                PropertyInfo info = plist.Find(p => p.Name == dt.Columns[i].ColumnName);
                if (info != null)
                {
                    try
                    {
                        if (!Convert.IsDBNull(dt.Rows[0][i]))
                        {
                            object v = null;
                            if (info.PropertyType.ToString().Contains("System.Nullable"))
                            {
                                v = Convert.ChangeType(dt.Rows[0][i], Nullable.GetUnderlyingType(info.PropertyType));
                            }
                            else
                            {
                                v = Convert.ChangeType(dt.Rows[0][i], info.PropertyType);
                            }
                            info.SetValue(s, v, null);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("字段[" + info.Name + "]转换出错," + ex.Message);
                    }
                }
            }
            return s;
        }
        /// <summary>
        /// 0.02会报错
        /// </summary>
        /// <param name="Money"></param>
        /// <returns></returns>
        public static string MoneyToCharacter(decimal Money)
        {
            string temp = "";
            string resu = "";
            string jf = "";
            int j = 0;
            int j_1 = 0;
            int jiao = 0;
            int fen = 0;
            int len = 0;
            List<string> Num = new List<string>();
            List<string> A = new List<string>();
            Num.Add("零");
            Num.Add("壹");
            Num.Add("贰");
            Num.Add("叁");
            Num.Add("肆");
            Num.Add("伍");
            Num.Add("陆");
            Num.Add("柒");
            Num.Add("捌");
            Num.Add("玖");
            A.Add("分");
            A.Add("角");
            A.Add("元");
            A.Add("拾");
            A.Add("佰");
            A.Add("仟");
            A.Add("萬");
            A.Add("拾");
            A.Add("佰");
            A.Add("仟");
            A.Add("億");
            A.Add("拾");
            A.Add("佰");
            A.Add("仟");
            temp = ((Math.Truncate(Math.Round(Money * 100))).ToString()).Trim();
            len = temp.Length;
            resu = "";
            if (len > 13 || len == 0)
                return "";
            jiao = Convert.ToInt32(temp.Substring(len - 2, 1));
            fen = Convert.ToInt32(temp.Substring(len - 1, 1));
            if (fen == 0)
            {
                if (jiao == 0)
                    jf = "整";
                else
                    jf = Num[jiao] + "角整";
            }
            else
            {
                if (jiao == 0)
                    jf = "零" + Num[fen] + "分";
                else
                    jf = Num[jiao] + "角" + Num[fen] + "分";
            }
            for (int i = 0; i < len - 2; i++)
            {
                j = Convert.ToInt32(temp.Substring(i, 1));//取第一位数字
                if (j == 0)
                {
                    j_1 = Convert.ToInt32(temp.Substring(i + 1, 1));//取第二位数字
                    if (j_1 == 0)
                        continue;
                    if (A[len - i - 1] == "萬" || A[len - i - 1] == "億")
                        resu = resu + A[len - i - 1] + Num[j];
                    else
                    {
                        if (A[len - i - 1] == "元")
                            resu = resu + "元";
                        else
                            resu = resu + Num[j];
                    }
                }
                else
                    resu = resu + Num[j] + A[len - i - 1];
            }
            return resu + jf;
        }

        /// <summary>
        /// 有用
        /// </summary>
        /// <param name="money"></param>
        /// <returns></returns>
        public static string GetChinaMoney(decimal money)
        {
            string[] strArray;
            string str = "";
            string str2 = "";
            string str3 = money.ToString("0.00");
            switch (str3.Trim().Length)
            {
                case 4:
                    strArray = new string[] { str3[0].ToString(), "y", str3[2].ToString(), "j", str3[3].ToString(), "f" };
                    str = string.Concat(strArray);
                    break;

                case 5:
                    strArray = new string[] { str3[0].ToString(), "s", str3[1].ToString(), "y", str3[3].ToString(), "j", str3[4].ToString(), "f" };
                    str = string.Concat(strArray);
                    break;

                case 6:
                    strArray = new string[] { str3[0].ToString(), "b", str3[1].ToString(), "s", str3[2].ToString(), "y", str3[4].ToString(), "j", str3[5].ToString(), "f" };
                    str = string.Concat(strArray);
                    break;

                case 7:
                    strArray = new string[] { str3[0].ToString(), "q", str3[1].ToString(), "b", str3[2].ToString(), "s", str3[3].ToString(), "y", str3[5].ToString(), "j", str3[6].ToString(), "f" };
                    str = string.Concat(strArray);
                    break;

                case 8:
                    strArray = new string[] { str3[0].ToString(), "w", str3[1].ToString(), "q", str3[2].ToString(), "b", str3[3].ToString(), "s", str3[4].ToString(), "y", str3[6].ToString(), "j", str3[7].ToString(), "f" };
                    str = string.Concat(strArray);
                    break;

                case 9:
                    strArray = new string[] { str3[0].ToString(), "s", str3[1].ToString(), "w", str3[2].ToString(), "q", str3[3].ToString(), "b", str3[4].ToString(), "s", str3[5].ToString(), "y", str3[7].ToString(), "j", str3[8].ToString(), "f" };
                    str = string.Concat(strArray);
                    break;

                case 10:
                    strArray = new string[] {
                        str3[0].ToString(), "b", str3[1].ToString(), "s", str3[2].ToString(), "w", str3[3].ToString(), "q", str3[4].ToString(), "b", str3[5].ToString(), "s", str3[6].ToString(), "y", str3[8].ToString(), "j",
                        str3[9].ToString(), "f"
                     };
                    str = string.Concat(strArray);
                    break;

                case 11:
                    strArray = new string[] {
                        str3[0].ToString(), "q", str3[1].ToString(), "b", str3[2].ToString(), "s", str3[3].ToString(), "w", str3[4].ToString(), "q", str3[5].ToString(), "b", str3[6].ToString(), "s", str3[7].ToString(), "y",
                        str3[9].ToString(), "j", str3[10].ToString(), "f"
                     };
                    str = string.Concat(strArray);
                    break;

                case 12:
                    strArray = new string[] {
                        str3[0].ToString(), "m", str3[1].ToString(), "q", str3[2].ToString(), "b", str3[3].ToString(), "s", str3[4].ToString(), "w", str3[5].ToString(), "q", str3[6].ToString(), "b", str3[7].ToString(), "s",
                        str3[8].ToString(), "y", str3[10].ToString(), "j", str3[11].ToString(), "f"
                     };
                    str = string.Concat(strArray);
                    break;
            }
            for (int i = 0; i < str.Trim().Length; i++)
            {
                switch (str[i])
                {
                    case '0':
                        str2 = str2 + "零";
                        break;

                    case '1':
                        str2 = str2 + "壹";
                        break;

                    case '2':
                        str2 = str2 + "贰";
                        break;

                    case '3':
                        str2 = str2 + "叁";
                        break;

                    case '4':
                        str2 = str2 + "肆";
                        break;

                    case '5':
                        str2 = str2 + "伍";
                        break;

                    case '6':
                        str2 = str2 + "陆";
                        break;

                    case '7':
                        str2 = str2 + "柒";
                        break;

                    case '8':
                        str2 = str2 + "捌";
                        break;

                    case '9':
                        str2 = str2 + "玖";
                        break;

                    case 'b':
                        str2 = str2 + "佰";
                        break;

                    case 'f':
                        str2 = str2 + "分";
                        break;

                    case 'j':
                        str2 = str2 + "角";
                        break;

                    case 'm':
                        str2 = str2 + "亿";
                        break;

                    case 'q':
                        str2 = str2 + "仟";
                        break;

                    case 's':
                        str2 = str2 + "拾";
                        break;

                    case 'w':
                        str2 = str2 + "万";
                        break;

                    case 'y':
                        str2 = str2 + "元";
                        break;
                }
            }
            return str2;
        }

        public static string GetMacAddressGPT()
        {
            string macAddress = string.Empty;

            NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();

            foreach (NetworkInterface networkInterface in networkInterfaces)
            {
                if (networkInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet ||
                    networkInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
                {
                    macAddress += networkInterface.GetPhysicalAddress().ToString();
                }
            }

            return macAddress;
        }

    }
}
